var express = require('express');
var router = express.Router();
var { movie_model, user_model } = require("../utils/model")
var { aesDecrypt } = require("../utils/index")

// Router express 路由模块  根据不同的url 去匹配对应的路由 加载对应的资源 
// get get 请求 
// req 请求 前端发给后端的请求 url headers
// res 响应  后端返回给前端的数据响应
// res.send   返回数据 string json 
// res.render(path,data)  渲染一个ejs 页面  data 传入到页面的参数  
// res.json 
/* GET home page. */
router.get('/', function (req, res, next) {
  console.log(req.session)
  res.render('index',
    {
      title: '千锋管理体系',
      username: req.session.username
    }
  );
});

router.get("/login", (req, res) => {
  var username = req.query.username ? aesDecrypt(req.query.username) : req.query.username;  // 解密
  res.render("login", {
    username
  })
})

router.get("/register", (req, res) => {
  res.render("register")
})

router.get("/mine", (req, res) => {
  res.render("mine")
})

router.get("/logout", (req, res) => {
  // 退出登录  
  req.session.destroy(() => {
    res.redirect("/")
  })
})

router.get("/list", (req, res) => {
  const query = req.query;
  console.log(query)

  var obj = {}
  var sort = {}
  var keyword = query.keyword
  if (keyword) {
    // 搜索
    obj = {
      $or: [
        {
          title: new RegExp(keyword)
        },
        {
          year: new RegExp(keyword)
        },
        {
          genres: new RegExp(keyword)
        },
      ]
    }
  } else {
    // 排序
    sort = query;
  }

  movie_model.find(obj, { _id: 0 })
    .sort(sort)
    .then(result => {
      // console.log(result)
      res.render("list", {
        result: result
      })
    })
    .catch(err => {
      res.send("<script>alert('服务器异常');location.href='/list'</script>")
    })
})

// router.get("/mv", (req, res) => {
//   movie_model.findOne({
//     id: req.query.mid
//   })
//     .then(result => {
//       res.render("movie", {
//         result
//       })
//     })
//     .catch(err => {
//       res.send("<script>alert('服务器异常');location.href='/list'</script>")
//     })

// })



// router.get("/list", (req, res) => {
//   res.render("list")
// })




router.get("/chat", (req, res) => {
  res.render("chat")
})









router.get("/home", (req, res) => {
  res.render("home", {
    msg: "wh2110-daydayup",
    count: 8899,
    word: "转介绍请给 zuozuomu (肥左)",
    wx: "zkl8970076",
    flag: !!0,
    tag: "<h1>Hello Wh2110  一定要精通Node </h1>",
    username: "",
    list: ["左帅", "老余", "肥拉", "小胡"]
  })
})

router.get("/demo", (req, res) => {
  res.send("<h2>wuhan2110-说好的人人交项目</h2>")
})

router.get("/data", (req, res) => {
  res.json({
    code: 200,
    type: 1,
    msg: "获取数据成功"
  })
})

router.get("/some", (req, res) => {
  res.json({
    code: 400,
    msg: "some - some -some ",
    result: null
  })
})



// 路由参数 
router.get("/todos/:who/:time?", (req, res) => {
  res.json({
    url: req.url,
    headers: req.headers,
    query: req.query,  // ? 拼接的查询参数 
    params: req.params,  // :  路由参数 动态路由 
    msg: "获取 req 的参数 "
  })
})

router.get("/movie", (req, res) => {
  movie_model.find({}, {})
    .limit(req.query.limit * 1)
    .then(result => {
      res.json({
        code: 200,
        msg: "获取电影数据成功",
        result: result,
        query: req.query
      })
    })
})

// 最基本的Node接口 
router.get("/userlist", (req, res) => {
  user_model.find()
    .then(result => {
      res.json({
        code: 200,
        msg: "获取用户成功",
        result
      })
    })
})


// post 请求 
router.post("/submit", (req, res) => {
  res.json({
    code: 200,
    msg: "提交成功",
    body: req.body,  // post 提交的参数  
    result: null
  })
})

// all = (post + get )
router.all("/allway", (req, res) => {
  res.send("这是 一个  post && get 请求 ")
})





module.exports = router;
